Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
Checking if form input exists [Django]

I have a Django form that a user enters one input into. After submitting they are taken to a URL associated with their input.

The database contains user_input and associated_url fields.

The user_input has the value unique=True set. When a duplicate input is entered the following displays on the website This value user_input already exists in the database even though I didn't set this so it. This may be feature of form.is_valid()?

So it immediately recognizes duplicate values, but I am trying to set it so that if a duplicate value is entered then it will just take the user to the associated_url for that value.

i.e.

Existing database table:

user_input    associated_url

hello     https://stackoverflow.com

If a user were to input hello into the form again, it would immedietly take them to https://stackoverflow.com instead of showing This value user_input already exists in the database

Code:

def Primary(request):

    form = Form()

    if request.method == 'POST':

        form = Form(request.POST)

        if form.is_valid():

                ... saving to database, redirect etc.

I need to be able to check if the input value is a duplicate. If it is then get the associated_url from the same row as the duplicate user_input entry and redirect the user there.

To do this myself I attempted adding an else statement to the form.is_valid condition, but that seems like bad practice because it will attempt to redirect regardless of the error(?) and because I don't know how to get the associated_url associated with the input value.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You have multiple options for this but some of them are.

param = request.POST["param"]

if YourModel.objects.get(param=param):
    redirect('/myurl/{}'.format(param))
else:
    if form.is_valid():
        # other code

Or write your own custom clean() method for the form you're using.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!